function [controlpoint_num, controlpoint_x,controlpoint_y,crossInterestBox,interest_xmin,interest_xmax,interest_ymin,interest_ymax] = fgenerateRandomSplineCoord()
    % imaging you have a rectangle area
    % you have a patch within it that is of interest for
    % current paths. The interest region is fixed
    % however you have the freedom to choose 
    % the bigger rectangle area randomly
    rng("shuffle")
    interest_xmin = 0; % unit in mm, this is fixed
    interest_xmax = 3;
    interest_ymin = 0;
    interest_ymax = 3;
    area_xmin_lb = -2; % unit in mm, lower boundary
    area_xmin_ub = -1;
    area_xmin_rangepts = 11; % partitions
    area_xmin_choices = linspace(area_xmin_lb,area_xmin_ub,area_xmin_rangepts);
    area_xmax_lb = 4; % repeat for xmax range
    area_xmax_ub = 5;
    area_xmax_rangepts = 11;
    area_xmax_choices = linspace(area_xmax_lb,area_xmax_ub,area_xmax_rangepts);
    area_ymin_choices = area_xmin_choices; % repeat for y boundary
    area_ymax_choices = area_xmax_choices;
    area_xmin = area_xmin_choices(randperm(area_xmin_rangepts,1)); % randomly generate the box
    area_xmax = area_xmax_choices(randperm(area_xmax_rangepts,1));
    area_ymin = area_ymin_choices(randperm(area_xmin_rangepts,1)); % randomly generate the box
    area_ymax = area_ymax_choices(randperm(area_xmax_rangepts,1));

    % now imagine you partition the big box in both x and y
    % you randomly choose number of control points
    % you randomly assign x for every control points in increasing x order
    % you randomly assign y for each associated x
    % you must at least have two control points to draw a line
    % for now, you have at most 51 control points
    % but if you generate more complex spline without self-interaction
    % the control point upper bound can increase
    grid_xnum = 16;
    grid_ynum = 51;
    x_choices = linspace(area_xmin,area_xmax,grid_xnum);
    y_choices = linspace(area_ymin,area_ymax,grid_ynum);
    rand_controlnum = randperm(grid_xnum,1);
    if rand_controlnum == 1
        rand_controlnum = 2; % at least need 2 control points
    end
    x_chosen = zeros(1,rand_controlnum);
    y_chosen = zeros(1,rand_controlnum);
        % for now the first and last control point has to lie at boundary
    if rand_controlnum == 2
        rand_xindex(1) = 1; % the first index must be left boundary
        rand_xindex(end) = grid_xnum; % the last index must be right boundary
    else
        rand_xindex = sort(randperm(grid_xnum-2, rand_controlnum-2)+1);
        rand_xindex = [1,rand_xindex,grid_xnum];
    end
        % now generate a random index list to chose x values from
        % we know that the boundary two points must be selected
        % instead of choosing index from [1,2,...,grid_xnum]
        % we only need to choose from [2,...grid_xnum-1];
        % so we first shift interval to [1,2...,grid_xnum-2]
        % we add +1 to every element to shift the interval to [2,3,4...,grid_xnum-1]
        % note that because we don't need to choose left and right boundary
        % index so the rand_controlnum-2 as well
        % randperm samples without replacement
    if rand_controlnum == 2
        %  in this case, x_1 and x_2 must be two boundaries
        x_chosen = [x_choices(1),x_choices(end)];
        y_chosen = [y_choices(randperm(grid_ynum,1)),y_choices(randperm(grid_ynum,1))];
    else
        for i = 1:rand_controlnum
            x_chosen(i) = x_choices(rand_xindex(i));
            y_chosen(i) = y_choices(randperm(grid_ynum,1));
        end
    end

    % now draw use a natural cubic spline to connect these control points
    % reference see wiki/matlab doc and de Boor pracical guide to splines
    % essentially, connecting splines are sets of piecewise second-order-smooth parametric curves
    spline_param = spline(x_chosen,y_chosen);

    % now we need to know if the spline ever crosses our center interest box
    % if not we set a flag and do not return these spline coordinate sets
    % we use a brute force method: scan x across interest box, if its
    % intersection with spline is within interest box y boundaries. Then there
    % is corssing and terminate calculation
    crossInterestBox = 1;
    fineScanNum = 201;
    fineScanX = linspace(interest_xmin, interest_xmax, fineScanNum);
    intersectionY = ppval(spline_param,fineScanX);
    yminchecks = intersectionY>interest_ymin;
    ymaxchecks = intersectionY<interest_ymax;
    if max(yminchecks+ymaxchecks) == 2
        crossInterestBox = 1;
    end
    controlpoint_num = rand_controlnum;
    controlpoint_x = x_chosen;
    controlpoint_y = y_chosen;
end

